#include "formmemberregistration.h"
#include "ui_formmemberregistration.h"
#include <QDebug>
#include <QMessageBox>

FormMemberRegistration::FormMemberRegistration(QSqlDatabase *db, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormMemberRegistration),
    dbase(db)
{
    ui->setupUi(this);
    setWindowTitle(tr("Member registration"));
    ui->labelFindMember->setPixmap(QIcon::fromTheme("edit-find-user").pixmap(16,16));
    ui->toolButtonReg->setIcon(QIcon::fromTheme("user-group-new"));
    ui->toolButtonUnReg->setIcon(QIcon::fromTheme("user-group-delete"));
    ui->toolButtonNewMember->setIcon(QIcon::fromTheme("list-add-user"));
    connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), SLOT(clickButton(QAbstractButton*)));
}

FormMemberRegistration::~FormMemberRegistration()
{
    delete ui;
}

void FormMemberRegistration::on_lineEditFindMember_textEdited(const QString &arg1)
{
    /// \todo implict set mask to baseMembers table
}

void FormMemberRegistration::on_toolButtonNewMember_clicked()
{
    /// @todo implict add new user to base and reload base model
}

void FormMemberRegistration::on_toolButtonReg_clicked()
{
    /// @todo Copy selected members to reg model and hide in base model
}

void FormMemberRegistration::on_toolButtonUnReg_clicked()
{
    /// @todo remove selected members in reg model and show in base model
}

bool FormMemberRegistration::updateData()
{
    /// @todo update
    qWarning() << tr("upadte data function not implicted");
    return false;
}

bool FormMemberRegistration::commitData()
{
    /// @todo commit
    qWarning() << tr("commit data function not implicted");
    return false;
}

void FormMemberRegistration::clickButton(QAbstractButton *button)
{
    if (button->text() == tr("Apply")) {
        commitData();
    } else if (button->text() == tr("Reset")) {
        updateData();
    } else {
        qWarning() << tr("Undeclared action");
    }
}

void FormMemberRegistration::showEvent(QShowEvent *event)
{
    setEnabled(dbase->isOpen());
    if (dbase->isOpen()) {
    } else {
        QMessageBox::critical(this, tr("Database error"), tr("No connection to database"), QMessageBox::Close, QMessageBox::Close);
    }
    event->accept();
}
